package host.haiwai.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

/**
 * swagger 文档配置
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    // 配置 swagger bean docker实例
    @Bean
    public Docket docket(Environment environment) {
        //设置要显示的环境
        Profiles profiles = Profiles.of("dev", "test");
        boolean flag = environment.acceptsProfiles(profiles);

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(flag) //上线后，一定要关闭此功能
                .select()
                .apis(RequestHandlerSelectors.basePackage("host.haiwai.controller")) //指定要扫描的包
                .build();
    }

    // 配置 swagger apiInfo 信息
    public ApiInfo apiInfo() {
        Contact contact = new Contact("joefche / JC","http://haiwai.host","joefche@qq.com");
        return new ApiInfo(
                "Springboot CURD 后台脚手架",
                "这是一个开源的Springboot后台权限框架",
                "1.0.0",
                "http://haiwai.host",
                 contact,
                "Apache2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>()
        );
    }
}
